import { request } from "../../request/index.js";
import regeneratorRuntime from '../../lib/runtime/runtime';
// pages/goods_list/index.js
Page({
  data: {
    res1:{},
    tabs:[
      {
        id:0,
        value:"综合",
        isActive:true
      },
      {
        id:1,
        value:"销量",
        isActive:false
      },   
      {
        id:2,
        value:"价格",
        isActive:false
      }
    ],
    goodsList:[]
  },
  //接口要的参数
  QueryParams:{
    query:"",
    cid:"",
    pagenum:1,
    pagesize:10
  },
  totalPages:1,

  onLoad: function (options) {
     this.QueryParams.cid=options.cid||"";
     this.QueryParams.query=options.query||"";
     this.getGoodsList();
  },
//获取商品列表
  async getGoodsList(){
    var that =this;
    var goodCid = this.QueryParams.cid;
    wx.request({
      url: 'http://localhost/gym/public/admin/api/goodsearch',
      data:{
        cid: goodCid
      },
      header:{
        "content-type": "application/json"
      },
      success(res){
        that.setData({res1:res.data});
        //获取总条数
        const total = that.data.res1.total;
         //计算总页数
        that.totalPages = Math.ceil(total / that.QueryParams.pagesize);
        that.setData({
          //拼接数组
           goodsList:[...that.data.goodsList,...that.data.res1.goods]
        })
      }
    })
    //关闭下啦刷新窗口
    wx.stopPullDownRefresh();
  },
//标签点击事件
  handleTabsItemChange(e){
    const { index }=e.detail;
    let {tabs}=this.data;
    tabs.forEach((v,i)=>i===index?v.isActive=true:v.isActive=false);
    this.setData({
      tabs
    })
  },
//页面上滑触底事件
  onReachBottom(){
   if(this.QueryParams.pagenum>=this.totalPages){
    wx.showToast({title:'已经到底了'});
   }else{
     this.QueryParams.pagenum++;
     this.getGoodsList();
   }
  },
  //下拉刷新事件
  onPullDownRefresh(){
    this.setData({
      goodsList:[]
    })
      this.QueryParams.pagenum=1;
      this.getGoodsList();
  }
})